<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="https://www.wulihub.com.cn/gc/Wez0VG/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="https://www.wulihub.com.cn/gc/Wez0VG/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="https://www.wulihub.com.cn/gc/Wez0VG/images/favicon-16x16.png">
  <link rel="mask-icon" href="https://www.wulihub.com.cn/gc/Wez0VG/images/logo.svg" color="#222">

<link rel="stylesheet" href="https://www.wulihub.com.cn/gc/Wez0VG/css/main.css">



<link rel="stylesheet" href="https://npm.elemecdn.com/@fortawesome/fontawesome-free@6.1.1/css/all.min.css" integrity="sha256-DfWjNxDkM94fVBWx1H5BMMp0Zq7luBlV8QRcSES7s+0=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://npm.elemecdn.com/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"nobige.cn","root":"/","images":"https://www.wulihub.com.cn/gc/Wez0VG/images","scheme":"Gemini","darkmode":false,"version":"8.12.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="https://www.wulihub.com.cn/gc/Wez0VG/js/config.js"></script>

    <meta name="description" content="需求 内网（断网、无互联网）的几台服务器老是时间提前，一开始延时几秒后面慢慢变的几分钟，影响数据。 找管理员也是效果不理想，管理员那边是通过交换机的时间，然后母鸡通过vmtools强制推送到客户端上，交换机的时间也是人为设定的，也出现时间提前的情况。 内网内无NTP服务器  架构 光闸（网闸）摆渡互联网时间接口（垃圾光闸不支持TCP、UDP转发） 内网通脚本定时同步并搭建NTP服务，供其他服务器">
<meta property="og:type" content="article">
<meta property="og:title" content="NTP内网时间同步方案chronyc">
<meta property="og:url" content="https://nobige.cn/post/20240513-nei_wang_shi_jian_tong_bu_fang_an/index.html">
<meta property="og:site_name" content="NoBige-JackCh3n">
<meta property="og:description" content="需求 内网（断网、无互联网）的几台服务器老是时间提前，一开始延时几秒后面慢慢变的几分钟，影响数据。 找管理员也是效果不理想，管理员那边是通过交换机的时间，然后母鸡通过vmtools强制推送到客户端上，交换机的时间也是人为设定的，也出现时间提前的情况。 内网内无NTP服务器  架构 光闸（网闸）摆渡互联网时间接口（垃圾光闸不支持TCP、UDP转发） 内网通脚本定时同步并搭建NTP服务，供其他服务器">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://blog-static.nobige.cn/gh/JackCh3n/n0bige/post/20240513-nei_wang_shi_jian_tong_bu_fang_an/sheng-hu-lnQ_qxiD5bA-unsplash.jpg">
<meta property="article:published_time" content="2024-05-13T06:31:16.000Z">
<meta property="article:modified_time" content="2024-11-07T02:59:06.338Z">
<meta property="article:author" content="JackCh3n">
<meta property="article:tag" content="centos">
<meta property="article:tag" content="内网">
<meta property="article:tag" content="ntp">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://blog-static.nobige.cn/gh/JackCh3n/n0bige/post/20240513-nei_wang_shi_jian_tong_bu_fang_an/sheng-hu-lnQ_qxiD5bA-unsplash.jpg">


<link rel="canonical" href="https://nobige.cn/post/20240513-nei_wang_shi_jian_tong_bu_fang_an/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"https://nobige.cn/post/20240513-nei_wang_shi_jian_tong_bu_fang_an/","path":"post/20240513-nei_wang_shi_jian_tong_bu_fang_an/","title":"NTP内网时间同步方案chronyc"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>NTP内网时间同步方案chronyc | NoBige-JackCh3n</title>
  
  <script class="next-config" data-name="google_analytics" type="application/json">{"tracking_id":"UA-122817633-1","only_pageview":true}</script>
  <script src="https://www.wulihub.com.cn/gc/Wez0VG/js/third-party/analytics/google-analytics.js"></script>

  <script src="https://www.wulihub.com.cn/gc/Wez0VG/js/third-party/analytics/baidu-analytics.js"></script>
  <script async src="https://hm.baidu.com/hm.js?96741f92c07d010c0ddd1d02197893e2"></script>




  <noscript>
    <link rel="stylesheet" href="https://www.wulihub.com.cn/gc/Wez0VG/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">NoBige-JackCh3n</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">copy to my blog</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home fa-fw"></i>Home</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="user fa-fw"></i>About</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags fa-fw"></i>Tags</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th fa-fw"></i>Categories</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive fa-fw"></i>Archives</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="sitemap fa-fw"></i>Sitemap</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9C%80%E6%B1%82"><span class="nav-number">1.</span> <span class="nav-text">需求</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%B6%E6%9E%84"><span class="nav-number">2.</span> <span class="nav-text">架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%92%E8%81%94%E7%BD%91"><span class="nav-number">3.</span> <span class="nav-text">互联网</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#NTP-docker"><span class="nav-number">3.1.</span> <span class="nav-text">NTP - docker</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%BB%BAchrony-NTP-Service"><span class="nav-number">3.2.</span> <span class="nav-text">自建chrony NTP  Service</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AE%89%E8%A3%85-Chrony-%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="nav-number">3.2.1.</span> <span class="nav-text">安装 Chrony 服务器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE-Chrony-%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="nav-number">3.2.2.</span> <span class="nav-text">配置 Chrony 服务器</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E9%98%B2%E7%81%AB%E5%A2%99"><span class="nav-number">3.2.3.</span> <span class="nav-text">配置防火墙</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Windows%E9%85%8D%E7%BD%AE%E5%91%BD%E4%BB%A4"><span class="nav-number">3.2.4.</span> <span class="nav-text">Windows配置命令</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%97%B6%E9%97%B4API"><span class="nav-number">4.</span> <span class="nav-text">时间API</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%86%85%E7%BD%91%E8%84%9A%E6%9C%AC"><span class="nav-number">5.</span> <span class="nav-text">内网脚本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%80%E5%90%8E"><span class="nav-number">6.</span> <span class="nav-text">最后</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%82%E8%80%83"><span class="nav-number">7.</span> <span class="nav-text">参考</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="JackCh3n"
      src="https://www.wulihub.com.cn/gc/Wez0VG/images/avatar.png">
  <p class="site-author-name" itemprop="name">JackCh3n</p>
  <div class="site-description" itemprop="description">Record the pit filling process with words</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">146</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">279</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/jackch3n" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;jackch3n" rel="noopener" target="_blank"><i class="github fa-fw"></i>GitHub</a>
      </span>
  </div>


  <div class="links-of-blogroll site-overview-item animated">
    <div class="links-of-blogroll-title"><i class="fa fa-globe fa-fw"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://vercel.com/?f=nobige" title="https:&#x2F;&#x2F;vercel.com?f&#x3D;nobige" rel="noopener" target="_blank">vercel</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.jsdelivr.com/?f=nobige" title="https:&#x2F;&#x2F;www.jsdelivr.com?f&#x3D;nobige" rel="noopener" target="_blank">jsdelivr</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://www.upyun.com/?f=nobige" title="https:&#x2F;&#x2F;www.upyun.com?f&#x3D;nobige" rel="noopener" target="_blank">upyun</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://coding.net/?f=nobige" title="https:&#x2F;&#x2F;coding.net?f&#x3D;nobige" rel="noopener" target="_blank">coding</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://unsplash.com/?f=nobige" title="https:&#x2F;&#x2F;unsplash.com?f&#x3D;nobige" rel="noopener" target="_blank">unsplash</a>
        </li>
    </ul>
  </div>

        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="https://nobige.cn/post/20240513-nei_wang_shi_jian_tong_bu_fang_an/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://www.wulihub.com.cn/gc/Wez0VG/images/avatar.png">
      <meta itemprop="name" content="JackCh3n">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="NoBige-JackCh3n">
      <meta itemprop="description" content="Record the pit filling process with words">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="NTP内网时间同步方案chronyc | NoBige-JackCh3n">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          NTP内网时间同步方案chronyc
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2024-05-13 14:31:16" itemprop="dateCreated datePublished" datetime="2024-05-13T14:31:16+08:00">2024-05-13</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2024-11-07 10:59:06" itemprop="dateModified" datetime="2024-11-07T10:59:06+08:00">2024-11-07</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%BB%8F%E9%AA%8C/" itemprop="url" rel="index"><span itemprop="name">经验</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="Views" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">Views: </span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p><img src="https://blog-static.nobige.cn/gh/JackCh3n/n0bige/post/20240513-nei_wang_shi_jian_tong_bu_fang_an/sheng-hu-lnQ_qxiD5bA-unsplash.jpg" alt="sheng-hu-lnQ_qxiD5bA-unsplash.jpg"></p>
<h2 id="需求"><a href="#需求" class="headerlink" title="需求"></a>需求</h2><blockquote>
<p>内网（断网、无互联网）的几台服务器老是时间提前，一开始延时几秒后面慢慢变的几分钟，影响数据。</p>
<p>找管理员也是效果不理想，管理员那边是通过交换机的时间，然后母鸡通过vmtools强制推送到客户端上，交换机的时间也是人为设定的，也出现时间提前的情况。</p>
<p>内网内无NTP服务器</p>
</blockquote>
<h2 id="架构"><a href="#架构" class="headerlink" title="架构"></a>架构</h2><ul>
<li>光闸（网闸）摆渡互联网时间接口<em>（垃圾光闸不支持TCP、UDP转发）</em></li>
<li>内网通脚本定时同步并搭建NTP服务，供其他服务器使用</li>
</ul>
<h2 id="互联网"><a href="#互联网" class="headerlink" title="互联网"></a>互联网</h2><ul>
<li><strong>NTP Service</strong></li>
<li>协议：<ul>
<li>UDP</li>
</ul>
</li>
<li>端口：<ul>
<li>123</li>
</ul>
</li>
<li>软件<ul>
<li>chrony</li>
</ul>
</li>
<li>NTP公共服务器<ul>
<li>ntp.tencent.com</li>
<li>ntp.aliyun.com</li>
</ul>
</li>
</ul>
<h3 id="NTP-docker"><a href="#NTP-docker" class="headerlink" title="NTP - docker"></a>NTP - docker</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">docker run -e TZ=Asia/Shanghai -d --restart=always \</span><br><span class="line">            -p 123:123/udp \</span><br><span class="line">            --name ntp \</span><br><span class="line">            --<span class="built_in">env</span> NTP_SERVERS=ntp.tencent.com,ntp.aliyun.com \</span><br><span class="line">            --<span class="built_in">env</span> LOG_LEVEL=0 \</span><br><span class="line">            -v /etc/timezone:/etc/timezone:ro \</span><br><span class="line">            -v /etc/localtime:/etc/localtime:ro \</span><br><span class="line">            --log-opt max-size=10m \</span><br><span class="line">            --log-opt max-file=1 \</span><br><span class="line">            cturra/ntp:latest</span><br><span class="line"></span><br><span class="line"><span class="comment"># 检查 ntp 服务器的时间源</span></span><br><span class="line">docker <span class="built_in">exec</span> ntp chronyc sources</span><br></pre></td></tr></table></figure>

<h3 id="自建chrony-NTP-Service"><a href="#自建chrony-NTP-Service" class="headerlink" title="自建chrony NTP  Service"></a>自建chrony NTP  Service</h3><p>在安装 Chrony 之前，需要在服务器中设置正确的时区。可以使用以下命令进行设置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">timedatectl set-timezone Asia/Kolkata</span><br></pre></td></tr></table></figure>

<p>现在，使用以下命令验证您当前的时区：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">timedatectl</span><br></pre></td></tr></table></figure>

<p>应该得到以下输出：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[root@localhost home]<span class="comment"># timedatectl</span></span><br><span class="line">      Local time: 一 2024-05-13 14:44:32 CST</span><br><span class="line">  Universal time: 一 2024-05-13 06:44:32 UTC</span><br><span class="line">        RTC time: 一 2024-05-13 06:44:51</span><br><span class="line">       Time zone: Asia/Shanghai (CST, +0800)</span><br><span class="line">     NTP enabled: <span class="built_in">yes</span></span><br><span class="line">NTP synchronized: no</span><br><span class="line"> RTC <span class="keyword">in</span> <span class="built_in">local</span> TZ: no</span><br><span class="line">      DST active: n/a</span><br></pre></td></tr></table></figure>

<h4 id="安装-Chrony-服务器"><a href="#安装-Chrony-服务器" class="headerlink" title="安装 Chrony 服务器"></a>安装 Chrony 服务器</h4><p>默认情况下，从 Centos 7.x 开始的最小发行版中都已经预装并开启了 Chrony，如果你的系统没有安装，可以用下面命令进行安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum -y install chrony</span><br></pre></td></tr></table></figure>

<p>安装 Chrony 后，启动 Chrony 服务并使其在系统重启时启动：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 开启服务</span></span><br><span class="line">systemctl start chronyd</span><br><span class="line"><span class="comment"># 设置自动开机</span></span><br><span class="line">systemctl <span class="built_in">enable</span> chronyd</span><br><span class="line"><span class="comment"># 查看状态</span></span><br><span class="line">systemctl status chronyd</span><br></pre></td></tr></table></figure>

<h4 id="配置-Chrony-服务器"><a href="#配置-Chrony-服务器" class="headerlink" title="配置 Chrony 服务器"></a>配置 Chrony 服务器</h4><p>Chronys 的主要配置文件位于 &#x2F;etc&#x2F;chrony.conf。需要对其进行编辑并更改时间服务器。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">vi /etc/chrony.conf</span><br></pre></td></tr></table></figure>

<p>注释掉默认池并添加离您的位置最近的 NTP 服务器列表。</p>
<blockquote>
<p>主要修改</p>
<p>server</p>
<p>allow</p>
<p>local stratum 10</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Use public servers from the pool.ntp.org project.</span></span><br><span class="line"><span class="comment"># Please consider joining the pool (http://www.pool.ntp.org/join.html).</span></span><br><span class="line">server ntp.tencent.com iburst</span><br><span class="line">server ntp.aliyun.com iburst</span><br><span class="line"><span class="comment">#server ntp.myinspurcloud.com iburst</span></span><br><span class="line"><span class="comment">#server 0.centos.pool.ntp.org iburst</span></span><br><span class="line"><span class="comment">#server 1.centos.pool.ntp.org iburst</span></span><br><span class="line"><span class="comment">#server 2.centos.pool.ntp.org iburst</span></span><br><span class="line"><span class="comment">#server 3.centos.pool.ntp.org iburst</span></span><br><span class="line"><span class="comment"># 内网无法联网使用，只开启本身其他全注释</span></span><br><span class="line"><span class="comment"># server 192.168.79. iburst</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Record the rate at which the system clock gains/losses time.</span></span><br><span class="line">driftfile /var/lib/chrony/drift</span><br><span class="line"></span><br><span class="line"><span class="comment"># Allow the system clock to be stepped in the first three updates</span></span><br><span class="line"><span class="comment"># if its offset is larger than 1 second.</span></span><br><span class="line">makestep 1.0 3</span><br><span class="line"></span><br><span class="line"><span class="comment"># Enable kernel synchronization of the real-time clock (RTC).</span></span><br><span class="line">rtcsync</span><br><span class="line"></span><br><span class="line"><span class="comment"># Enable hardware timestamping on all interfaces that support it.</span></span><br><span class="line"><span class="comment">#hwtimestamp *</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Increase the minimum number of selectable sources required to adjust</span></span><br><span class="line"><span class="comment"># the system clock.</span></span><br><span class="line"><span class="comment">#minsources 2</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Allow NTP client access from local network.</span></span><br><span class="line"><span class="comment">#allow 192.168.0.0/16</span></span><br><span class="line"><span class="comment"># 允许全部访问</span></span><br><span class="line">allow all</span><br><span class="line"><span class="comment"># Serve time even if not synchronized to a time source.</span></span><br><span class="line"><span class="comment"># 即使未与server时间源同步，也提供时间。</span></span><br><span class="line"><span class="built_in">local</span> stratum 10</span><br><span class="line"></span><br><span class="line"><span class="comment"># Specify file containing keys for NTP authentication.</span></span><br><span class="line"><span class="comment">#keyfile /etc/chrony.keys</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Specify directory for log files.</span></span><br><span class="line">logdir /var/log/chrony</span><br><span class="line"></span><br><span class="line"><span class="comment"># Select which information is logged.</span></span><br><span class="line"><span class="comment">#log measurements statistics tracking</span></span><br><span class="line"><span class="comment">#bindcmdaddress 0.0.0.0</span></span><br><span class="line"><span class="comment">#bindcmdaddress ::</span></span><br><span class="line"><span class="comment">#bindcmdaddress /var/run/chrony/chronyd.sock</span></span><br></pre></td></tr></table></figure>
<p>开启服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 完成后保存并关闭文件，然后使用以下命令设置 NTP 同步：</span></span><br><span class="line">timedatectl set-ntp <span class="literal">true</span></span><br><span class="line"><span class="comment"># 接下来，重新启动 Chrony 服务以应用更改：</span></span><br><span class="line">systemctl restart chronyd</span><br><span class="line"><span class="comment"># 使用以下命令检查的 NTP 服务器是否正常工作：</span></span><br><span class="line">chronyc sources -v</span><br><span class="line"><span class="comment"># 查看时间信息</span></span><br><span class="line"><span class="built_in">date</span></span><br><span class="line"><span class="comment"># 查看详细的时间信息</span></span><br><span class="line">timedatectl</span><br></pre></td></tr></table></figure>

<h4 id="配置防火墙"><a href="#配置防火墙" class="headerlink" title="配置防火墙"></a>配置防火墙</h4><p>需要允许 NTP 服务通过防火墙。可以使用以下命令允许它：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --permanent --add-port=123/udp</span><br></pre></td></tr></table></figure>

<p>接下来，重新加载防火墙服务以应用更改：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">firewall-cmd --reload</span><br></pre></td></tr></table></figure>

<h4 id="Windows配置命令"><a href="#Windows配置命令" class="headerlink" title="Windows配置命令"></a>Windows配置命令</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 自建</span></span><br><span class="line">w32tm /config /syncfromflags:manual /manualpeerlist:<span class="string">&quot;10.26.8.61&quot;</span></span><br><span class="line"><span class="comment"># 互联网</span></span><br><span class="line">w32tm /config /syncfromflags:manual /manualpeerlist:<span class="string">&quot;ntp.tencent.com&quot;</span></span><br><span class="line"><span class="comment"># 配置更新</span></span><br><span class="line">w32tm /config /update</span><br><span class="line"><span class="comment"># 同步</span></span><br><span class="line">w32tm /resync</span><br></pre></td></tr></table></figure>



<h2 id="时间API"><a href="#时间API" class="headerlink" title="时间API"></a>时间API</h2><p>搭建在互联网的Chrony那台服务器上，因为有装openresty就直接用了</p>
<figure class="highlight nginx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 北京时间</span></span><br><span class="line"><span class="comment"># 2024-05-13 15:08:10</span></span><br><span class="line"><span class="section">location</span> /time &#123;</span><br><span class="line">    <span class="attribute">default_type</span> <span class="string">&#x27;text/plain&#x27;</span>;</span><br><span class="line">    <span class="section">content_by_lua_block</span> &#123;</span><br><span class="line">        ngx.print(os.date(&quot;!%Y-%m-%<span class="attribute">d</span> %H:%M:%S<span class="string">&quot;, os.time() + 8 * 3600))</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="内网脚本"><a href="#内网脚本" class="headerlink" title="内网脚本"></a>内网脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/bin/bash</span></span><br><span class="line"><span class="comment"># Get the time from your API</span></span><br><span class="line">TIME=$(curl -s http://172.12.34.56:12000/time | <span class="built_in">tr</span> -d <span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># Check if time is empty or not</span></span><br><span class="line"><span class="keyword">if</span> [ -z <span class="string">&quot;<span class="variable">$TIME</span>&quot;</span> ]</span><br><span class="line"><span class="keyword">then</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">&quot;Failed to fetch time from API!&quot;</span></span><br><span class="line">  <span class="built_in">exit</span> 1</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">  <span class="built_in">echo</span> <span class="string">&quot;Fetched time from API: <span class="variable">$TIME</span>&quot;</span></span><br><span class="line"><span class="keyword">fi</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Set the system time</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;Setting system time...&quot;</span></span><br><span class="line">sudo <span class="built_in">date</span> -s <span class="string">&quot;<span class="variable">$TIME</span>&quot;</span></span><br><span class="line"><span class="built_in">echo</span> <span class="string">&quot;Done!&quot;</span></span><br></pre></td></tr></table></figure>

<p>配置一下cron，每隔几分钟自动执行一次</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">*/5 * * * * /bin/bash /home/updatesystemtime.sh</span><br></pre></td></tr></table></figure>

<h2 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h2><p>如此可以达到内网时间同步情况，但还是依赖于互联网（光闸摆渡）条件，如果完全离线的话需要定期手动调整时间，不然时间误差会在1分钟左右。</p>
<h2 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h2><ul>
<li><a target="_blank" rel="noopener" href="https://cn.linux-console.net/?p=3043">https://cn.linux-console.net/?p=3043</a></li>
<li><a target="_blank" rel="noopener" href="https://baijiahao.baidu.com/s?id=1748465951244135308">https://baijiahao.baidu.com/s?id=1748465951244135308</a></li>
<li><a target="_blank" rel="noopener" href="https://unsplash.com/photos/a-person-standing-in-the-middle-of-a-field-under-a-night-sky-filled-with-stars-lnQ_qxiD5bA">https://unsplash.com/photos/a-person-standing-in-the-middle-of-a-field-under-a-night-sky-filled-with-stars-lnQ_qxiD5bA</a></li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/centos/" rel="tag"># centos</a>
              <a href="/tags/%E5%86%85%E7%BD%91/" rel="tag"># 内网</a>
              <a href="/tags/ntp/" rel="tag"># ntp</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/20240320-VN007+_chao_ji_mi_ma_ji_suan_qi_suan_fa/" rel="prev" title="VN007+超级密码计算器算法">
                  <i class="fa fa-chevron-left"></i> VN007+超级密码计算器算法
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/20240522-PPDuck3_po_jie_suan_fa_jie_xi/" rel="next" title="PPDuck3破解算法解析">
                  PPDuck3破解算法解析 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    
  <div class="comments" id="disqus_thread">
    <noscript>Please enable JavaScript to view the comments powered by Disqus.</noscript>
  </div>
  
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">JackCh3n</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>
    <br/>Powerful CDN service provided by 
  <a target="_blank" rel="noopener" href="https://www.upyun.com/?utm_source=lianmeng&utm_medium=referral" style="border:none"><img src="https://www.wulihub.com.cn/gc/Wez0VG/images/upyun_logo.png" height="66" style="display: list-item;" alt="upyun"></a> 
  <a target="_blank" rel="noopener" href="https://www.jsdelivr.com" style="border:none"><img src="https://www.wulihub.com.cn/gc/Wez0VG/images/jsdelivr_logo.svg" height="50" style="display: list-item;" alt="JsDelivr"></a>
  <p>
  <br/><a target="_blank" rel="noopener" href="https://pages.github.com/?f=nobige">Github Pages</a>&nbsp;&nbsp;<a target="_blank" rel="noopener" href="https://vercel.com/?f=nobige">Vercel</a>&nbsp;&nbsp;<a target="_blank" rel="noopener" href="https://www.netlify.com/?f=nobige">Netlify</a>&nbsp;&nbsp;<a target="_blank" rel="noopener" href="https://coding.net/?f=nobige">coding</a>&nbsp;&nbsp;provides static publishing
  </p>

    </div>
  </footer>

  
  <script src="https://npm.elemecdn.com/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="https://www.wulihub.com.cn/gc/Wez0VG/js/comments.js"></script><script src="https://www.wulihub.com.cn/gc/Wez0VG/js/utils.js"></script><script src="https://www.wulihub.com.cn/gc/Wez0VG/js/motion.js"></script><script src="https://www.wulihub.com.cn/gc/Wez0VG/js/next-boot.js"></script>

  
<script src="https://npm.elemecdn.com/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="https://www.wulihub.com.cn/gc/Wez0VG/js/third-party/search/local-search.js"></script>





  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




<link rel="stylesheet" href="https://npm.elemecdn.com/disqusjs@1.3.0/dist/disqusjs.css" integrity="sha256-GxdCIOyfxQ1OBfS99qAIJDoGK1ADuBsxhMTqXG82fAY=" crossorigin="anonymous">

<script class="next-config" data-name="disqusjs" type="application/json">{"enable":true,"api":"https://duosuo.nobige.cn/","apikey":"Kmo9kPwsir7ppp8tGpeGROCmUtW8tdZBOdBVowPC67xVfTS5uV1wr6s4kckvP95m","shortname":"nobige","js":{"url":"https://npm.elemecdn.com/disqusjs@1.3.0/dist/disqus.js","integrity":"sha256-LVaMHPQ2zLqOc5rXSAfr4d1PIkEGNLyyUTDNPZmTtUw="}}</script>
<script src="https://www.wulihub.com.cn/gc/Wez0VG/js/third-party/comments/disqusjs.js"></script>

</body>
</html>
